package com.kgc.controller;

import com.kgc.bean.User;
import com.kgc.bean.UserVO;
import com.kgc.config.annotation.RequestPermission;
import com.kgc.config.annotation.RequestUser;
import com.kgc.service.IntegralSettlementService;
import com.kgc.service.UserService;
import com.kgc.util.RedisUtils;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * Create on 2021/6/21
 * <p>
 * Author YANQINGCHAO
 * <p>
 * Description:
 */
@RestController
@Api(tags="用户信息维护")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private IntegralSettlementService integralSettlementService;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;

    /**
    * @author YANQINGCHAO
    * @date 2021/6/21
    * @return
    * @descrption： 进行积分结算
    */
    @RequestPermission
    @GetMapping("/toIntegralSettlement")
    @ApiOperation(value = "进行积分结算",notes = "根据用户购买商品支付的实际金额进行积分结算，10块钱兑换1积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name="actualPayment",required = true,value = "实际付款金额" )
    })

    public ReturnResult<String> toIntegralSettlement(@RequestUser @ApiIgnore User user, @RequestParam Integer actualPayment){
        log.info("调用积分结算");
        Integer score=integralSettlementService.integralSettlement(user.getUserId(),actualPayment);
        if(score>=0){
            log.info("积分结算结束");
            return ResultBuildUtil.success("积分结算成功，本次结算的积分为："+score);
        }
        return ResultBuildUtil.fail("7623","积分兑换失败");
    }
    /**
    * @author YANQINGCHAO
    * @date 2021/6/21
    * @return
    * @descrption： 根据用户id查询用户信息
    */
    @RequestPermission
    @GetMapping("/queryUserInfo")
    @ApiOperation(value = "查询用户信息")
    public ReturnResult<UserVO> queryUserInfo(@RequestUser @ApiIgnore User user){
        return ResultBuildUtil.success(userService.queryUserById(user.getUserId()));
    }
    /**
    * @author YANQINGCHAO
    * @date 2021/6/23
    * @return
    * @descrption： 签到
    */
    @RequestPermission
    @GetMapping("toSignIn")
    @ApiOperation(value = "签到",notes = "用户进行签到，获得对应积分，如果用户今天已经签到，则会提示今天已经签到完毕，不能继续签到")
    public ReturnResult<String> toSignIn(@RequestUser @ApiIgnore User user){

        Date date = new Date();
        Integer getScore =userService.signIn(date,user.getUserId());
        if(getScore>0){
            return  ResultBuildUtil.success("签到成功，本次签到获取的积分为："+getScore);
        }

        return ResultBuildUtil.fail("6001","今天已经签到完毕，不能继续签到");

    }
    /**
     * @author YANQINGCHAO
     * @date 2021/6/23
     * @return
     * @descrption： 收藏处理
     */
    @RequestPermission
    @GetMapping("collect")
    @ApiOperation(value = "收藏",notes = "用户点击收藏，如果在收藏表中存在此商品的收藏信息，则会将其从收藏表中删除，不存在则会将此商品添加到收藏表中，不允许连续多次进行收藏操作")
    @ApiImplicitParam(name="goodsId",required = true,value = "商品id" )
    public ReturnResult<String> collectResolve(@RequestUser @ApiIgnore User user, @RequestParam String goodsId){

        if(redisUtils.checkFreq("flag",2,10)){
            String info="";
            Integer result=userService.collectResolve(goodsId,user.getUserId());
            if(result==1){
                info+="取消收藏成功";

            }else if(result==2){
                info+="移入收藏成功";
            }else{
                info+="操作执行失败";
            }
            return ResultBuildUtil.success(info);
        }else{
            return ResultBuildUtil.fail("6002","收藏处理操作过于频繁，请十秒后再进行操作");
        }

    }


}
